// models/AnalyticsEvent.js
const mongoose = require('mongoose');

const analyticsEventSchema = new mongoose.Schema({
    userId: { type: String, required: true, index: true }, // UV 标识
    sessionId: { type: String, required: true, index: true },
    pageViewId: { type: String, required: true, unique: true }, // PV 标识
    eventType: { type: String, required: true, enum: ['pageview', 'pageleave', 'heartbeat', 'customevent'], index: true },
    url: { type: String, required: true },
    timestamp: { type: Date, required: true, default: Date.now, index: true },
    referrer: { type: String },
    screenWidth: { type: Number },
    screenHeight: { type: Number },
    language: { type: String },
    duration: { type: Number }, // 仅用于 pageleave 事件
    activeDuration: { type: Number }, // 仅用于 heartbeat 事件
    // 可以添加更多自定义字段
    customData: { type: mongoose.Schema.Types.Mixed }
}, { timestamps: true }); // timestamps 会自动添加 createdAt 和 updatedAt 字段

module.exports = mongoose.model('AnalyticsEvent', analyticsEventSchema);